<template>
  <div style="height:100%;width:100%;position:relative;">
      <div class="pagetop" ref="buttondiv">
          <table>
              <tr>
                  <td style="padding-left:5px;">序列号代码</td>
                  <td style="padding-left:5px; width:300px;">
                      <el-input v-model="tablename" @keyup.enter.native="query" id="inputCode" width="300px"></el-input>
                  </td>
                  <td style="padding-left:5px;">
                      <el-button type="primary" icon="el-icon-search" @click="query()" v-has="'PINQUERY'">查询</el-button>
                  </td>
                  <td style="padding-left:5px;">
                      <el-button icon="el-icon-check" @click="checkTable()" v-has="'PINQUERY'">验证</el-button>
                  </td>
                  <td style="padding-left:5px;">
                      <el-button icon="el-icon-menu" @click="save()" v-has="'PMODIFY'">修复</el-button>
                  </td>
              </tr>
          </table>
      </div>
      <div class="pagecenter">
          <div ref="xTableDiv" style="height:99%;width:100%">
              <vxe-table show-overflow ref="xTable1" height="auto" width="100%" :loading="loading" :select-config="xTable1CheckAll" :data.sync="tableData1" border size="mini" :row-class-name="rowClassName" :keyboard-config="keyConfig" show-header-overflow highlight-current-row highlight-hover-row resizable stripe>
                  <vxe-table-column type="selection" width="40" align="center"></vxe-table-column>
                  <!-- <vxe-table-column
          type="index"
          title="NO."
          width="60"
          align="center"
        ></vxe-table-column> -->
                  <vxe-table-column field="seq_code" title="序列号代码" width="200" header-align="center">
                  </vxe-table-column>
                  <vxe-table-column field="is_loop" title="循环使用" width="80" header-align="center" align="center">
                  </vxe-table-column>
                  <vxe-table-column field="max_val" title="允许最大值" width="100" align="center"></vxe-table-column>
                  <vxe-table-column field="now_val" title="当前值" width="100" align="center"></vxe-table-column>
                  <vxe-table-column field="cp_now_max" title="当前表ID" width="100" align="center">
                  </vxe-table-column>
                  <vxe-table-column field="cp_table_cnt" title="当前表记录数" width="100" align="center">
                  </vxe-table-column>
                  <vxe-table-column field="cp_update" title="是否需要修复" width="100" align="center">
                  </vxe-table-column>
                  <vxe-table-column field="cp_msg" title="相关提示" width="200" align="center">
                  </vxe-table-column>
              </vxe-table>
          </div>
      </div>
  </div>
</template>

<script type="text/javascript">
import XEUtils from "xe-utils"; //引入工具类
XEUtils.mixin({
  // 自定义全局的格式化处理函数
  formatSelect(cellValue, list) {
      let item = list.find((item) => item.value == cellValue);
      return item ? item.label : "";
  },
});
export default {
  name: "MNG190",
  data() {
      return {
          loading: false,
          qcruleDialogVisible: false,
          instrId: 0,
          tableData1: [],
          xTable1CheckAll: { checkALl: false },
          tablename: "",
          keyConfig: {
              isArrow: true,
              isDel: true,
              isTab: true,
              isEdit: true,
              editMethod: ({ seq, row, rowIndex, column, columnIndex, cell }) => {},
          },
      };
  },

  //html加载完成之前，执行。执行顺序：父组件-子组件
  created() {
      this.initialList(); //初始化主列表
  },
  //.html加载完成后执行。执行顺序：子组件-父组件
  mounted() {},
  updated() {},
  methods: {
      initialList() {
          //this.query();
      },

      query() {
          //主表查询
          this.loading = true;

          this.$tool
              .request({
                  url: "MNG/MNG1/MNG190",
                  method: "post",
                  data: {
                      action: "query",
                      tablename: this.tablename,
                  },
              })
              .then((response) => {
                  this.tableData1 = response.Data;
                  this.loading = false;
              })
              .catch((e) => {
                  this.loading = false;
                  console.log("error", e);
              });
      },
      checkTable() {
          var tables = [];
          var selectRows = this.$refs.xTable1.getSelectRecords();
          if (selectRows) {
              for (let index in selectRows) {
                  var item = {};
                  item.seq_code = selectRows[index].seq_code;
                  item.tb = selectRows[index].tb;
                  tables[index] = item;
              }
          } else {
              return;
          }
          this.$tool
              .request({
                  url: "MNG/MNG1/MNG190",
                  method: "post",
                  data: {
                      action: "checkTable",
                      tables: tables,
                  },
              })
              .then((response) => {
                  var data = response.Data;
                  for (var i = 0; i < data.length; i++) {
                      var dt = this.$refs.xTable1.getTableData().fullData;
                      for (var m = 0; m < dt.length; m++) {
                          if (data[i]["seq_code"] == dt[m]["seq_code"] && data[i]["tb"] == dt[m]["tb"]) {
                              dt[m]["now_val"] = data[i]["now_val"];
                              dt[m]["cp_table_cnt"] = data[i]["cp_table_cnt"];
                              if (data[i]["cp_now_max"] != dt[m]["now_val"]) {
                                  dt[m]["cp_now_max"] = data[i]["cp_now_max"];
                                  if (data[i]["cp_now_max"] > dt[m]["now_val"] && (data[i]["cp_now_max"] < dt[m]["max_val"] || dt[m]["max_val"] == null)) dt[m]["cp_update"] = "是";
                              } else {
                                  dt[m]["cp_now_max"] = "正常";
                              }
                          }
                      }
                  }
              })
              .catch((e) => {
                  this.loading = false;
                  console.log("error", e);
              });
      },

      save() {
          var tables = [];
          var selectRows = this.$refs.xTable1.getSelectRecords();
          if (selectRows) {
              for (let index in selectRows) {
                  if (selectRows[index].cp_update == "是") {
                      var item = {};
                      item.seq_code = selectRows[index].seq_code;
                      item.tb = selectRows[index].tb;
                      tables.push(item);
                  }
              }
          } else {
              return;
          }
          this.$tool
              .request({
                  url: "MNG/MNG1/MNG190",
                  method: "post",
                  data: {
                      action: "save",
                      tables: tables,
                  },
              })
              .then((response) => {
                  var data = response.Data;
                  for (var i = 0; i < data.length; i++) {
                      var dt = this.$refs.xTable1.getTableData().fullData;
                      for (var m = 0; m < dt.length; m++) {
                          if (data[i]["seq_code"] == dt[m]["seq_code"] && data[i]["tb"] == dt[m]["tb"]) {
                              if (data[i]["cp_msg"] == "" && data[i]["cp_flag"] > 0) {
                                  dt[m]["cp_msg"] = "修复成功";
                              } else {
                                  dt[m]["cp_msg"] = data[i]["cp_msg"];
                              }
                          }
                      }
                  }
              })
              .catch((e) => {
                  this.loading = false;
                  console.log("error", e);
              });
      },
      rowClassName({ row, rowIndex }) {
          if (row.cp_update) {
              return "row-red";
          }
      },

      //////////////////////////////工具函数////////////////////////////////////
      getDataByAjax(
          request,
          okMsg,
          errMsg,
          okFunction, // 取得数据后的回调函数
          errFunction // 获取数据失败后回调函数
      ) {
          this.$tool.request(request).then((response) => {
              if (response.errcode == "1") {
                  this.$message({
                      message: okMsg,
                      type: "success",
                  });
                  if (typeof okFunction == "function") okFunction(response.Data);
              } else {
                  this.$message({
                      dangerouslyUseHTMLString: true,
                      message: errMsg + "<br><br>详细信息：" + response.errmsg,
                      type: "warning",
                  });
                  if (typeof errFunction == "function") errFunction(response.Data);
              }
          });
      },

      insertRowAtLast(xtable, row) {
          xtable.insertAt(row, -1); //在最后添加一行
          var curow = xtable.getTableData().fullData[xtable.getTableData().fullData.length - 1]; //获取 新加的行对象 row 数据集合
          xtable.setCurrentRow(curow); //高亮显示行
          this.$nextTick(() => {
              xtable.scrollToRow(curow); //待数据加载完成后，延迟 滚动 到行
          });
      },

      //自制必填校验函数，以后待扩展
      valid(obj, message) {
          if (XEUtils.isUndefined(obj)) {
              return this.$message({
                  message: message,
                  type: "warning",
              });
          }
          if (XEUtils.isNull(obj)) {
              return this.$message({
                  message: message,
                  type: "warning",
              });
          }
          if (XEUtils.isString(obj)) {
              if (!XEUtils.getSize(obj) > 0) {
                  return this.$message({
                      message: message,
                      type: "warning",
                  });
              }
          }
          if (XEUtils.isDate(obj)) {
              if (obj < XEUtils.toStringDate("2000-01-01")) {
                  return this.$message({
                      message: message,
                      type: "warning",
                  });
              }
          }
          return null;
      },

      deleteRow(xTable) {
          var delrow = xTable.getCurrentRow();
          this.DelIndex = xTable.getRowIndex(delrow);
          xTable.remove(delrow);
          if (this.DelIndex > -1 && xTable.tableData.length > 0) {
              if (xTable.getTableData().fullData.length > this.DelIndex) this.DelIndex = this.DelIndex;
              else this.DelIndex = this.DelIndex - 1;
              var selectrow = xTable.getTableData().fullData[this.DelIndex];
              this.$nextTick(() => {
                  xTable.setCurrentRow(selectrow);
              });
          } else {
              var nowdata = xTable.getTableData().fullData.filter((item) => {
                  return item._XID < delrow._XID;
              });
              if (nowdata.length > 0) {
                  var selectrow = nowdata[nowdata.length - 1];
                  this.$nextTick(() => {
                      xTable.setCurrentRow(selectrow);
                  });
              }
          }
      },
  },
};
</script>

<style scoped>
.pagetop {
  left: 0;
  right: 0;
  height: 36px;
  display: -webkit-box;
  -webkit-box-pack: left;
  -webkit-box-align: center;
  border: solid 1px #ccc;
  position: absolute;
  z-index: 99;
  text-align: left;
}
.pagebottom {
  width: 100%;
  height: 40px;
  display: -webkit-box;
  -webkit-box-pack: left;
  -webkit-box-align: left;
  position: absolute;
  bottom: 0px;
  font-size: 12px;
  border: solid 1px #ccc;
}
.pagecenter {
  position: absolute;
  top: 40px;
  width: 100%;
  left: 0;
  bottom: 0px;
  overflow: auto;
}
.middle-center {
  position: absolute;
  height: 100%;
  right: 485px;
  left: 0;
}

.middle-right {
  position: absolute;
  top: 0;
  bottom: 2px;
  overflow: visible;
  width: 480px;
  right: 0px;
}
::v-deep tr.vxe-body--row.row-red {
  color: red;
}
</style>
